local mingcha = fk.CreateSkill{
  name = "hanqing__mingcha",
}

Fk:loadTranslationTable{
  ["hanqing__mingcha"] = "明察",
  [":hanqing__mingcha"] = "每回合开始时，你可以将一名角色的至少X张手牌替换为【杀】或【闪】，然后其本回合结束时可以弃置一张牌发动一次〖制衡〗；"..
  "若其此回合抵消或响应了牌，你重置X（X为此技能发动次数）。",

  ["#hanqing__mingcha-choose"] = "明察：你可以将一名角色至少%arg张手牌替换为【杀】或【闪】，其本回合结束时可以弃一张牌发动“制衡”",
  ["#hanqing__mingcha-ask"] = "明察：将 %dest 至少%arg张手牌替换为【杀】或【闪】",
  ["#hanqing__mingcha-invoke"] = "明察：你可以弃一张牌，发动“制衡”",
}

mingcha:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(mingcha.name) and
      table.find(player.room.alive_players, function (p)
        return p:getHandcardNum() > player:usedSkillTimes(mingcha.name, Player.HistoryGame)
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return p:getHandcardNum() > player:usedSkillTimes(mingcha.name, Player.HistoryGame)
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = mingcha.name,
      prompt = "#hanqing__mingcha-choose:::"..(player:usedSkillTimes(mingcha.name, Player.HistoryGame) + 1),
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local cards = {}
    local n = player:usedSkillTimes(mingcha.name, Player.HistoryGame)
    if to == player then
      cards = room:askToCards(player, {
        min_num = n,
        max_num = 999,
        include_equip = false,
        skill_name = mingcha.name,
        prompt = "#hanqing__mingcha-ask::"..to.id..":"..n,
        cancelable = false,
      })
    else
      cards = room:askToChooseCards(player, {
        target = to,
        min = n,
        max = 999,
        flag = "h",
        skill_name = mingcha.name,
        prompt = "#hanqing__mingcha-ask::"..to.id..":"..n,
      })
    end
    local moves = {}
    table.insert(moves, {
      ids = cards,
      from = to,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonJustMove,
      skillName = mingcha.name,
      proposer = player,
      moveVisible = true,
    })
    local ids = room:getCardsFromPileByRule("slash,jink", #cards, "allPiles")
    if #ids > 0 then
      table.insert(moves, {
        ids = ids,
        to = to,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        skillName = mingcha.name,
        proposer = player,
        moveVisible = false,
      })
    else
      return
    end
    room:setPlayerMark(player, "hanqing__mingcha_target-turn", to.id)
    room:setPlayerMark(to, "hanqing__mingcha_invoke-turn", 1)
    room:moveCards(table.unpack(moves))
  end,
})

mingcha:addEffect(fk.TurnEnd, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:getMark("hanqing__mingcha_invoke-turn") > 0 and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local card = room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = mingcha.name,
      prompt = "#hanqing__mingcha-invoke",
      cancelable = true,
      skip = true,
    })
    if #card > 0 then
      event:setCostData(self, {cards = card})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self).cards, mingcha.name, player, player)
    if player.dead or not table.find(player:getCardIds("he"), function (id)
      return not player:prohibitDiscard(id)
    end) then return end
    room:askToUseActiveSkill(player, {
      skill_name = "hanqing__zhiheng",
      prompt = "#hanqing__zhiheng",
      cancelable = false,
    })
  end,
})

mingcha:addEffect(fk.AfterAskForCardUse, {
  can_refresh = function (self, event, target, player, data)
    return data.eventData and player:getMark("hanqing__mingcha_target-turn") == target.id and
      data.result and data.result.from == target
  end,
  on_refresh = function(self, event, target, player, data)
    player:setSkillUseHistory(mingcha.name, 0, Player.HistoryGame)
  end,
})

mingcha:addEffect(fk.AfterAskForCardResponse, {
  can_refresh = function (self, event, target, player, data)
    return data.eventData and player:getMark("hanqing__mingcha_target-turn") == target.id and data.result
  end,
  on_refresh = function(self, event, target, player, data)
    player:setSkillUseHistory(mingcha.name, 0, Player.HistoryGame)
  end,
})

mingcha:addEffect(fk.AfterAskForNullification, {
  can_refresh = function (self, event, target, player, data)
    return data.eventData and data.result and player:getMark("hanqing__mingcha_target-turn") == data.result.from.id
  end,
  on_refresh = function(self, event, target, player, data)
    player:setSkillUseHistory(mingcha.name, 0, Player.HistoryGame)
  end,
})

return mingcha
